package com.mmy.controller.ship;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.ship.TShipOrder;
import com.mmy.domain.ship.TShipPlan;
import com.mmy.domain.ship.VShipBase;
import com.mmy.domain.ship.VShipOrder;
import com.mmy.domain.ship.VShipPlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.ship.impl.ShipOrderServiceImpl;
import com.mmy.service.ship.impl.ShipPlanServiceImpl;
import com.mmy.service.ship.impl.TShipBaseServiceImpl;
import com.mmy.service.ship.impl.TShipOrderServiceImpl;
import com.mmy.service.ship.impl.TShipPlanServiceImpl;

/**
 * 类的介绍：船票订单
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class ShipOrderController {

	@Autowired
	TShipOrderServiceImpl tShipOrderServiceImpl;
	@Autowired
	TShipBaseServiceImpl tShipBaseServiceImpl;
	@Autowired
	TShipPlanServiceImpl tShipPlanServiceImpl;
	@Autowired
	ShipOrderServiceImpl shipOrderServiceImpl;
	@Autowired
	ShipPlanServiceImpl shipPlanServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 新建船票预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterreserveshipbase")
	public String EnterReserveShipBase(HttpServletRequest request, Model model) {

		int iShipID = Integer.parseInt(request.getParameter("iShipID"));

		List<VShipPlan> lstShipPlan = tShipPlanServiceImpl.findByShipID(iShipID);
		model.addAttribute("shipplanlist", lstShipPlan);

		model.addAttribute("iShipID", iShipID);

		return "webbackground/ship/reserveshipbase";

	}


	/**
	 * 函数功能说明： 预留转订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterpayordership")
	public String EnterPayOrderShip(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TShipOrder tShipOrder = tShipOrderServiceImpl.findByID(iID);
		model.addAttribute("tShipOrder", tShipOrder);

		int iShipID = Integer.parseInt(request.getParameter("iShipID"));
		model.addAttribute("iShipID", iShipID);

		return "webbackground/ship/payordership";

	}

	@RequestMapping(value = "/subpayordership")
	public String PayOrderShip(TShipOrder tShipOrder, HttpSession session, String ShipID) throws Exception {

		try {
//			 定义一个新的实体类对象 
			TShipOrder dtShipOrder = tShipOrderServiceImpl.findByID(tShipOrder.getID());

			dtShipOrder.setPayType(tShipOrder.getPayType());
			dtShipOrder.setOrderState((byte) 2);
			dtShipOrder.setOrderDate(new Date());
			dtShipOrder.setOrderName(tShipOrder.getOrderName());
			dtShipOrder.setOrderTel(tShipOrder.getOrderTel());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtShipOrder.setOrderUserID(tSysUser.getUserID());
			dtShipOrder.setReceivable(tShipOrder.getReceivable());
			dtShipOrder.setVoucher(tShipOrder.getVoucher());
			dtShipOrder.setReceipt(tShipOrder.getReceipt());
			tShipOrderServiceImpl.save(dtShipOrder);

			return "redirect:printship?iID=" + tShipOrder.getID();

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 取消订单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelordership")
	public String EnterCancelOrderShip(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TShipOrder tShipOrder = tShipOrderServiceImpl.findByID(iID);
		model.addAttribute("tShipOrder", tShipOrder);

		int iShipID = Integer.parseInt(request.getParameter("iShipID"));
		model.addAttribute("iShipID", iShipID);

		return "webbackground/ship/cancelordership";

	}

	@RequestMapping(value = "/subcancelordership")
	public String CancelOrderShip(TShipOrder tShipOrder, HttpSession session, String ShipID) throws Exception {

		try {
//			 定义一个新的实体类对象 
			TShipOrder dtShipOrder = tShipOrderServiceImpl.findByID(tShipOrder.getID());

			dtShipOrder.setOrderState((byte) 4);
			dtShipOrder.setRefund(tShipOrder.getRefund());
			tShipOrderServiceImpl.save(dtShipOrder);

			return "redirect:printcancelship?iID=" + tShipOrder.getID();

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 修改船票预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdatereserveship")
	public String EnterUpdateReserveShip(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TShipOrder tShipOrder = tShipOrderServiceImpl.findByID(iID);
		model.addAttribute("tShipOrder", tShipOrder);
		int iShipID = Integer.parseInt(request.getParameter("iShipID"));
		model.addAttribute("iShipID", iShipID);

		return "webbackground/ship/updatereserveship";

	}

	@RequestMapping(value = "/updatereserveship")
	public String UpdateReserveShipBase(TShipOrder tShipOrder, HttpSession session, String ShipID) throws Exception {
		try {
//			 定义一个新的实体类对象 
			TShipOrder dtShipOrder = tShipOrderServiceImpl.findByID(tShipOrder.getID());
			dtShipOrder.setReserveName(tShipOrder.getReserveName());
			dtShipOrder.setReserveTel(tShipOrder.getReserveTel());
			dtShipOrder.setMemo(tShipOrder.getMemo());
			dtShipOrder.setModifyDate(new Date());

			BigDecimal bPrice = dtShipOrder.getPrice();
			dtShipOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtShipOrder.setReviser(tSysUser.getUserName());
			tShipOrderServiceImpl.save(dtShipOrder);
			return "redirect:shipbaseinfo?iShipID=" + Integer.parseInt(ShipID);

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 取消预留
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelreserveship")
	public String CancelReserveShip(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TShipOrder tShipOrder = tShipOrderServiceImpl.findByID(iID);

		tShipOrder.setOrderState((byte) 3);

		tShipOrderServiceImpl.save(tShipOrder);

		model.addAttribute("tShipOrder", tShipOrder);
		int iShipID = Integer.parseInt(request.getParameter("iShipID"));
		model.addAttribute("iShipID", iShipID);

		VShipBase vShipBase = tShipBaseServiceImpl.findByID(iShipID);
		model.addAttribute("vShipBase", vShipBase);

		List<VShipOrder> lstShipOrder = tShipOrderServiceImpl.findByShipID(iShipID);
		model.addAttribute("orderlist", lstShipOrder);

		return "webbackground/ship/shipbaseinfo";

	}

	/**
	 * 函数功能说明： 修改船票订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdateordership")
	public String EnterUpdateOrderShip(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TShipOrder tShipOrder = tShipOrderServiceImpl.findByID(iID);
		model.addAttribute("tShipOrder", tShipOrder);
		int iShipID = Integer.parseInt(request.getParameter("iShipID"));
		model.addAttribute("iShipID", iShipID);

		return "webbackground/ship/updateordership";

	}

	@RequestMapping(value = "/updateordership")
	public String UpdateOrderShipBase(TShipOrder tShipOrder, HttpSession session, String ShipID) throws Exception {
		try {
//			 定义一个新的实体类对象 
			TShipOrder dtShipOrder = tShipOrderServiceImpl.findByID(tShipOrder.getID());
			dtShipOrder.setOrderName(tShipOrder.getOrderName());
			dtShipOrder.setOrderTel(tShipOrder.getOrderTel());
			dtShipOrder.setMemo(tShipOrder.getMemo());
			dtShipOrder.setModifyDate(new Date());

			BigDecimal bPrice = dtShipOrder.getPrice();
			dtShipOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtShipOrder.setReviser(tSysUser.getUserName());
			tShipOrderServiceImpl.save(dtShipOrder);
			return "redirect:shipbaseinfo?iShipID=" + Integer.parseInt(ShipID);

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除取消预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delreserveshipbase")
	public String DelReserveShipBase(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();

		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}

		tShipOrderServiceImpl.delShipOrder(lstID);

		int iShipID = Integer.parseInt(request.getParameter("iShipID"));
		model.addAttribute("iShipID", iShipID);
		VShipBase vShipBase = tShipBaseServiceImpl.findByID(iShipID);
		model.addAttribute("vShipBase", vShipBase);

		List<VShipOrder> lstShipOrder = tShipOrderServiceImpl.findByShipID(iShipID);
		model.addAttribute("orderlist", lstShipOrder);

		return "webbackground/ship/shipbaseinfo";
	}

	/**
	 * 函数功能说明： 删除取消订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delordershipbase")
	public String DelOrderShipBase(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();

		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}

		tShipOrderServiceImpl.delShipOrder(lstID);

		int iShipID = Integer.parseInt(request.getParameter("iShipID"));
		model.addAttribute("iShipID", iShipID);

		VShipBase vShipBase = tShipBaseServiceImpl.findByID(iShipID);
		model.addAttribute("vShipBase", vShipBase);

		List<VShipOrder> lstShipOrder = tShipOrderServiceImpl.findByShipID(iShipID);
		model.addAttribute("orderlist", lstShipOrder);

		return "webbackground/ship/shipbaseinfo";
	}

	/**
	 * 函数功能说明： 前台个人中心点击取消船票预留
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/entershipcancelreserve")
	public String enterShipCancelReserve(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		shipOrderServiceImpl.updateState(iID, sessionSysUser);
		return "redirect:myshipreserve";
	}

	/**
	 * 函数功能说明： 前台个人中心点击申请船票退单
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/entershipapplyrefund")
	public String enterShipApplyRefund(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		shipOrderServiceImpl.updateRefundState(iID, sessionSysUser);
		return "redirect:myshiporder";
	}

	/**
	 * 函数功能说明： 前台个人中心点击撤销船票退单
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/entershipcancelrefund")
	public String enterShipCancelRefund(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		shipOrderServiceImpl.updateCancelState(iID, sessionSysUser);
		return "redirect:myshiprefund";
	}

	/* 前台个人中心显示订单详情 */
	@RequestMapping(value = "/myshipinfo")
	public String toMyShipInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TShipOrder dbShipOrder = shipOrderServiceImpl.findByID(iID);
		model.addAttribute("dbShipOrder", dbShipOrder);

		String strOrderID = dbShipOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(6, 12));

		VShipPlan dbShipPlan = shipPlanServiceImpl.findByID(iPlanID);

		model.addAttribute("dbShipPlan", dbShipPlan);

		return "permanagement/MyShipInfo";
	}
}
